﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

using Microsoft.Xna.Framework.Graphics;
using TomShane.Neoforce.Controls;

using UnitShared;
using tactics.Commands;

namespace tactics.GUI
{
    using ScreenCoords = Utility.Pair<int, int>;
    using FormatMap = Dictionary<string, string>;

    public class MiniDisplay : IGUIParsable
    {
        Manager guiManager;
        StatusDisplayManager statusDisplay;
        string statusXmlFile;

        Panel backPanel;
        
        ImageBox portrait;
        TextBox name;
        TextBox health;

        Unit unit;
        Player player;
        FormatMap formats;
        ScreenCoords position;

        Color nameColour;

        #region Properties

        public ScreenCoords Position
        {
            get { return position; }
            set { UpdatePosition(value.First, value.Second);  }
        }

        public Unit Unit
        {
            get { return unit; }
            set { unit = value; }
        }

        public int Width
        {
            get { return backPanel.Width; }
        }

        public int Height
        {
            get { return backPanel.Height; }
        }

        public bool Visible
        {
            get { return backPanel.Visible; }
            set { backPanel.Visible = value; }
        }

        #endregion

        public MiniDisplay(Manager guiManagerIn, string xmlFile, string statusXmlFileIn, Unit unitIn, Player playerIn, Color nameColourIn)
        {
            statusXmlFile = statusXmlFileIn;
            InitControls(guiManagerIn, unitIn, playerIn, nameColourIn);
            GUIBuilder.BuildGUI(this, guiManager, xmlFile, "MiniDisplay");
            SetInfoDisplay();
        }

        public void Remove()
        {
            guiManager.Remove(backPanel);
            statusDisplay.Remove();
        }

        #region Initialize Controls

        private void InitControls(Manager guiManagerIn, Unit unitIn, Player playerIn, Color nameColourIn)
        {
            unit = unitIn;
            player = playerIn;

            guiManager = guiManagerIn;
            nameColour = nameColourIn;

            formats = new FormatMap();

            position = new ScreenCoords();

            backPanel = new Panel(guiManager);
            backPanel.Init();

            portrait = new ImageBox(guiManager);
            portrait.Init();
            portrait.SizeMode = SizeMode.Stretched;
            portrait.Click += new TomShane.Neoforce.Controls.EventHandler(portrait_Click);
            portrait.MouseOver += new MouseEventHandler(portrait_MouseOver);
            portrait.MouseOut += new MouseEventHandler(portrait_MouseOut);
            
            name = new TextBox(guiManager);
            name.Init();

            health = new TextBox(guiManager);
            health.CanFocus = false;
        }

        void portrait_MouseOut(object sender, MouseEventArgs e)
        {
            CommandQueue.Instance.AddCommand(new EnableDisableMouseCommand(true));
            this.backPanel.Top += 5;
            statusDisplay.Adjust(0, 5);
        }

        void portrait_MouseOver(object sender, MouseEventArgs e)
        {
            CommandQueue.Instance.AddCommand(new EnableDisableMouseCommand(false));
            this.backPanel.Top -= 5;
            statusDisplay.Adjust(0, -5);
        }

        void portrait_Click(object sender, TomShane.Neoforce.Controls.EventArgs e)
        {
            CommandQueue.Instance.AddCommand(new SelectUnitCommand(unit, player));
        }

        #endregion

        #region Display

        public void SetInfoDisplay()
        {
            portrait.Image = TacticsEngine.Instance.Content.Load<Texture2D>(unit.Texture);            

            ToolTip t = new ToolTip(guiManager);
            t.Text = string.Format(unit.Name, formats["Portrait"]);
            portrait.ToolTip = t;

            name.Text = string.Format(formats["Name"], unit.Name);

            if (!unit.Moved)
                name.Color = nameColour;
            else
                name.Color = Color.Gray;

            health.Text = string.Format(formats["Health"], unit.CurrentHp);
        }

        public void UpdatePosition(int x, int y)
        {
            backPanel.Left = x;
            backPanel.Top = y;

            position.First = x;
            position.Second = y;

            if (statusDisplay != null)
                statusDisplay.HideCards();

            statusDisplay = new StatusDisplayManager(guiManager, statusXmlFile, unit, StatusDisplayManager.LayoutOptions.Up2C, Position.First + 5, Position.Second - 2);
            statusDisplay.ShowCards();
        }

        #endregion

        #region Parsing Interface

        public Control Parse(XmlReader r, string controlName)
        {
            Control found = null;

            if (controlName == "BackPanel")
            {
                GUIBuilder.ParseControl<Panel>(r, backPanel);
                found = backPanel;
            }
            else if (controlName == "Portrait")
            {
                GUIBuilder.ParseControl<ImageBox>(r, portrait);
                formats[controlName] = portrait.ToolTip.Text;
                found = portrait;
            }
            else if (controlName == "Health")
            {
                GUIBuilder.ParseControl<TextBox>(r, health);
                formats[controlName] = health.Text;
                found = health;
            }
            else if (controlName == "Name")
            {
                GUIBuilder.ParseControl<TextBox>(r, name);
                formats[controlName] = name.Text;
                found = name;
            }

            return found;
        }

        #endregion

    }
}
